package com.sunchangpeng.client.bio;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TimeClient {
    private static final Logger LOGGER = LoggerFactory.getLogger(TimeClient.class);

    public void start() {
        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;

        try {
            socket = new Socket("127.0.0.1", 8080);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
            out.println("Time");
            String resp = in.readLine();
            LOGGER.info("Now is {}", resp);
        } catch (IOException e) {
            LOGGER.error("", e);
        } finally {
            if (out != null) {
                out.close();
                out = null;
            }

            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    LOGGER.error("", e);
                }
                in = null;
            }

            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    LOGGER.error("", e);
                }

                socket = null;
            }
        }
    }
}
